<?php

namespace common\models;

use backend\models\AdminUser;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "{{%check_log}}".
 *
 * @property integer $id
 * @property integer $mod_id
 * @property integer $mod_type
 * @property integer $admin_id
 * @property string $admin_group
 * @property string $remark
 * @property integer $status
 * @property integer $created_at
 * @property AdminUser $adminUser
 */
class CheckLog extends \yii\db\ActiveRecord
{
    
    private $_count = 0;
    /** @var Check */
    private $_check;
    
    const TYPE_CHECK = 1;
    
    public function behaviors(){
        return [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ]
            ]
        ];
    }
    
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%check_log}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['admin_id', 'admin_group', 'remark', 'status', 'mod_id'], 'required'],
            [['admin_id', 'status', 'created_at', 'mod_id', 'mod_type'], 'integer'],
            [['admin_group', 'remark'], 'string', 'max' => 255],
            ['status', 'validateAdmin'],
        ];
    }
    
    public function validateAdmin($attribute)
    {
        if( !$this->hasErrors() ){
            
            if( static::findOne(['mod_id' => $this->mod_id, 'mod_type' => $this->mod_type, 'admin_id' => $this->admin_id]) ){
                $this->addError($attribute, '不能重复审核');
                return false;
            }
            // 审核次数
            if( $this->getCheckCount() >= 3 || $this->getCheck()->times == -1 ){
                $this->addError($attribute, '已经审核过，无需审核');
                return false;
            }
        }
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '序号',
            'mod_id' => '申请记录',
            'admin_id' => '管理员',
            'admin_group' => '管理员身份',
            'remark' => '备注',
            'status' => '审核状态',
            'created_at' => '审核时间',
        ];
    }
    
    public function submit()
    {
        if( !$this->validate() ){
            return false;
        }
        
        // 验证审核次数
        if( $this->mod_type == static::TYPE_CHECK ){
            $class = Check::className();
        }
        /** @var Check $model */
        $model = $class::find()->where(['id' => $this->mod_id])->one();
        if( $this->status == 1 ){
            $model->times = -1;
        }else{
            $model->times = $model->times + 1;
        }
        $model->check_time = time();
        $model->save();
        
        $this->save();
    }
    
    public function getAdminUser()
    {
        return $this->hasOne(AdminUser::className(), ['id' => 'admin_id']);
    }
    
    public static function findByCheckMod($id)
    {
        return CheckLog::find()->where(['mod_id' => $id, 'mod_type' => static::TYPE_CHECK, 'admin_id' => Yii::$app->user->id])->one();
    }
    
    public static function findListByCheckMod($id)
    {
        return CheckLog::find()->where(['mod_id' => $id, 'mod_type' => static::TYPE_CHECK])->all();
    }
    
    private function getCheckCount()
    {
        if( $this->_count === null ){
            $this->_count = static::find()->where(['mod_type' => $this->mod_type, 'mod_id' => $this->mod_id])->count();
        }
        return $this->_count;
    }
    
    private function getCheck()
    {
        if( $this->_check === null ){
            if( $this->mod_type == static::TYPE_CHECK ){
                /** @var Check $model */
                $class = Check::className();
            }
            $this->_count = $class::find()->where(['id' => $this->mod_id])->one();
            
        }
        return $this->_check;
    }
    
}
